package com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.adapter;

import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.entity.WXDataEntity;

import java.util.List;

/**
 * @author quchao
 * @date 2018/2/24
 */

public class ArticleListAdapter extends BaseQuickAdapter<WXDataEntity.DataBean.DatasBean, ArticleListAdapter.WXViewHolder> {

    private boolean isCollectPage;
    private boolean isSearchPage;
    private boolean mHasRecorded;

    public ArticleListAdapter(int layoutResId, @Nullable List<WXDataEntity.DataBean.DatasBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final WXViewHolder helper, WXDataEntity.DataBean.DatasBean article) {
        if (!TextUtils.isEmpty(article.getTitle())) {
            helper.setText(R.id.item_search_pager_title, Html.fromHtml(article.getTitle()));
        }
        if (!TextUtils.isEmpty(article.getAuthor())) {
            helper.setText(R.id.item_search_pager_author, article.getAuthor());
        }
        if (!TextUtils.isEmpty(article.getChapterName())) {
            String classifyName = article.getSuperChapterName() + " / " + article.getChapterName();
            if (isCollectPage) {
                helper.setText(R.id.item_search_pager_chapterName, article.getChapterName());
            } else {
                helper.setText(R.id.item_search_pager_chapterName, classifyName);
            }
        }
        if (!TextUtils.isEmpty(article.getNiceDate())) {
            helper.setText(R.id.item_search_pager_niceDate, article.getNiceDate());
        }
        if (isSearchPage) {
            CardView cardView = helper.getView(R.id.item_search_pager_group);
            cardView.setForeground(null);
            cardView.setBackground(ContextCompat.getDrawable(mContext, R.color.card_color));
        }

        if (helper.getLayoutPosition() == 1 && !mHasRecorded) {
            mHasRecorded = true;
            helper.getView(R.id.item_search_pager_group).getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    helper.getView(R.id.item_search_pager_group).getViewTreeObserver().removeOnPreDrawListener(this);
                    return true;
                }
            });
        }

        helper.addOnClickListener(R.id.item_search_pager_chapterName);
    }

    public static class WXViewHolder extends BaseViewHolder{

        TextView tvAuthor;
        TextView tvChapterName;
        TextView tvTitle;
        TextView tvNiceDate;

        public WXViewHolder(View view) {
            super(view);
            tvAuthor = itemView.findViewById(R.id.item_search_pager_author);
            tvChapterName = itemView.findViewById(R.id.item_search_pager_chapterName);
            tvTitle = itemView.findViewById(R.id.item_search_pager_title);
            tvNiceDate = itemView.findViewById(R.id.item_search_pager_niceDate);
        }
    }

}
